package com.project.service;

import com.project.entity.StudentInfo;
import com.project.entity.TeacherInfo;
import com.project.entity.User;
import com.project.service.base.BaseService;
import com.project.service.base.CrpData.CrypDataHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * author: WheelChen
 * date: 2017/8/4
 * <p>
 * 学生相关操作
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class StudentServiceImpl extends BaseService implements StudentService {
    @Autowired
    private TeacherInfoService teacherInfoService;

    @Autowired
    private StudentInfoService studentInfoService;

    @Autowired
    private UserService userService;

    @Autowired
    private CrypDataHelper crypDataHelper;


    /**
     * author: WheelChen
     * date: 2017/8/4
     * function: 根据internAdvisorUserId获取该老师指导下的学生们的用户表信息（仅解密姓名）
     *
     * @param internAdvisorUserId 学校实习指导老师用户表id
     * @return
     */
    @Override
    public List<User> findBatchByInternAdvisorUserId(Integer internAdvisorUserId) throws Exception {
        //首先拿到教师信息表id
        TeacherInfo teacherInfo = teacherInfoService.findOneByUserId(internAdvisorUserId);
        if (teacherInfo != null) {
            Integer internAdvisor = teacherInfo.getId();
            StudentInfo studentInfo = new StudentInfo();
            studentInfo.setInternadvisor(internAdvisor);
            //查询到老师下指导学生的学生信息表
            List<StudentInfo> studentInfoList = studentInfoService.findBatchByCondition(studentInfo);
            //由实习信息表对应查出用户表
            List<User> userList = new ArrayList<>();
            for (StudentInfo studentInfo1 : studentInfoList) {
                User user = userService.findByUserIdUnDecry(studentInfo1.getUserid());
                //过滤敏感信息
                filterSensitiveInfo(user);
                if (StringUtils.isNotEmpty(user.getRealname()))
                    user.setRealname(crypDataHelper.decRealName(user.getRealname()));
                userList.add(user);
            }
            return userList;
        } else {
            return null;
        }
    }

    /**
     * author: WheelChen
     * date: 2017/7/28
     * function: 新增学生详细信息 （影响用户表 + 学生信息表）
     *
     * @param user 用户表信息
     * @param studentInfo 学生表信息
     * @return
     */
    @Override
    public boolean addDetailOne(User user, StudentInfo studentInfo) throws Exception {
        boolean flag = false;
        if (user != null) {
            int res = userService.createUser(user);
            studentInfo.setUserid(user.getId());
            int res2 = studentInfoService.add(studentInfo);
            if (res == 1 && res2 == 1) flag = true;
        } else {
            throw new Exception("用户表不存在");
        }
        return flag;
    }

    /**
     * author: WheelChen
     * date: 2017/9/18
     * function: 删除学生相关的所有信息（影响用户表、用人单位表、实习信息表、就业信息表、简历表、应聘表、实习日志表）
     *
     * @param studentInfoList
     * @return
     */
    @Override
    public boolean removeBatchChain(List<StudentInfo> studentInfoList) {
        List<Integer> userIdList = new ArrayList<>();
        List<Integer> studentInfoIdList = new ArrayList<>();
        //单独获取用户id列表、学生信息表id列表
        for (StudentInfo studentInfo : studentInfoList){
            userIdList.add(studentInfo.getUserid());
            studentInfoIdList.add(studentInfo.getId());
        }

        return true;
    }
}
